home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 2.8 KB | 122 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
-
- public class wordcollection extends chunkedtext {
-
- /**
- ** Constructors
- **/
- public wordcollection(StringBuffer inString){
- super(inString);
- }
-
- public wordcollection(String inString){
- super(inString);
- }
-
-
- /**
- ** Inherites the bulk of collection protocol from chunkedtext
- **/
-
-
- /**
- ** protected handlers
- **/
-
- protected boolean isChunkSeparator(char inCh){
- boolean found = false;
- for(int i = 0; (! found) && (i < mWordDelimiters.length()); i++ ){
- found = (inCh == mWordDelimiters.charAt(i));
- }
- return found;
- }
-
- protected char defaultChunkSeparator(){
- return ' ';
- }
-
-
- /**
- ** private parts
- **/
-
- private static final String mWordDelimiters = " .,–?!:;\n\t\\";
- private static final char mThousandsSeparator = ',';
- private static final char mDecimalPoint = '.';
-
-
- protected int[] nextChunk(int inLookingFrom) {
- // retruns an array of 2 integers, start and finish
- // inLookingFrom is the character index where we start looking
-
- int wordStart = stringLength(); //default vals
- int wordEnd = stringLength(); //default vals
- int i = inLookingFrom;
-
- if (inLookingFrom <= 1){ //if it's the very first time
- wordStart = 0;
- } else {
- for (; i < stringLength(); i++){
- //go untill we find a non-delimiter
- if (! (isChunkSeparator(getNthChar(i)))){
- wordStart = i;
- break;
- }
- }
- }
- for (; i < stringLength(); i++){
- //go untill we find a delimiter
- //(unless its a thousands Separator or decimel point)
- if (isChunkSeparator(getNthChar(i))
- && (! isDecimalPoint(i))
- && (! isThousandsSeparator(i))) {
- wordEnd = i;
- break;
- }
- }
- int returnVal[] = {wordStart, wordEnd};
- return returnVal;
- }
-
-
-
- private boolean isNumeral(char inCh){
- return ((inCh < '9') && (inCh > '0'));
- }
-
-
- private boolean isThousandsSeparator (int inCharacterIndex) {
- boolean returnVal = false;
- try {
- returnVal = ((mThousandsSeparator == getNthChar(inCharacterIndex))
- && (inCharacterIndex + 3 <= stringLength())
- && (isNumeral( getNthChar(++ inCharacterIndex)))
- && (isNumeral( getNthChar(++ inCharacterIndex)))
- && (isNumeral( getNthChar(++ inCharacterIndex))));
- }
- catch (RuntimeException e){
- returnVal = false;
- }
- return returnVal;
- }
-
-
- private boolean isDecimalPoint (int inCharacterIndex) {
- boolean returnVal = false;
- try {
- returnVal = ((mDecimalPoint == getNthChar(inCharacterIndex))
- && (inCharacterIndex + 1 <= stringLength())
- && (isNumeral( getNthChar(++ inCharacterIndex))));
- }
- catch (RuntimeException e){
- returnVal = false;
- }
- return returnVal;
- }
- }